//
//  SettingView.m
//  跑马灯
//
//  Created by huahua on 16/12/28.
//  Copyright © 2016年 os1. All rights reserved.
//

#import "SettingView.h"
#import "OrderListView.h"

#define kEdge 20  //下拉列表之间的间距
#define kSuperViewWidth self.bounds.size.width  //父视图宽度
#define kSuperViewHeight self.bounds.size.height  //父视图高度
#define kTextColorTitle @"TEXTCOLORTITLE"
#define kTextFontTitle @"TEXTFONTTITLE"
#define kBgColorTitle @"BGCOLORTITLE"
#define kTextColor @"TEXTCOLOR"
#define kBgColor @"BGCOLOR"

@interface SettingView ()<UITableViewDataSource,UITableViewDelegate>
{
    OrderListView *_textColorOrderList;  //文字颜色
    OrderListView *_textFontOrderList;  //文字字体
    OrderListView *_bgColorOrderList;  //背景颜色
    UIButton *_cancelBtn;  //取消按钮
    UIButton *_sureBtn;  //设定按钮
    UIControl *_textColorControl;
    UIControl *_textFontControl;
    UIControl *_bgColorControl;  // 覆盖在三个OrderListView上的UIControl
    UITableView *_textColorTableView;
    UITableView *_textFontTableView;
    UITableView *_bgColorTableView;
    
    NSArray *_colorArray;  //存储颜色数组
    NSArray *_fontArray;  //存储字体大小数组
    
    UIColor *_textColor;
    UIColor *_bgColor;
    NSInteger _textFont;
    
    BOOL _textColorSelect;
    BOOL _textFontSelect;
    BOOL _bgColorSelect;  //三个UIControl的选中状态
}
@end

@implementation SettingView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor grayColor];
        
        [self _setupDatas];
        
        [self _setupUIViewWithSuperViewFrame:frame];
    }
    return self;
}

/** 初始化数据*/
- (void)_setupDatas{
    
    //  默认黑底白字
    NSDictionary *colorDic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"white",@"name",[UIColor colorWithRed:1 green:1 blue:1 alpha:1],@"color", nil];
    NSDictionary *colorDic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"black",@"name",[UIColor blackColor],@"color", nil];
    NSDictionary *colorDic3 = [NSDictionary dictionaryWithObjectsAndKeys:@"red",@"name",[UIColor redColor],@"color", nil];
    NSDictionary *colorDic4 = [NSDictionary dictionaryWithObjectsAndKeys:@"green",@"name",[UIColor greenColor],@"color", nil];
    NSDictionary *colorDic5 = [NSDictionary dictionaryWithObjectsAndKeys:@"blue",@"name",[UIColor blueColor],@"color", nil];
    NSDictionary *colorDic6 = [NSDictionary dictionaryWithObjectsAndKeys:@"yellow",@"name",[UIColor yellowColor],@"color", nil];
    NSDictionary *colorDic7 = [NSDictionary dictionaryWithObjectsAndKeys:@"gray",@"name",[UIColor grayColor],@"color", nil];
    NSDictionary *colorDic8 = [NSDictionary dictionaryWithObjectsAndKeys:@"orange",@"name",[UIColor orangeColor],@"color", nil];
    _colorArray = [NSArray arrayWithObjects:colorDic1,colorDic2,colorDic3,colorDic4,colorDic5,colorDic6,colorDic7,colorDic8, nil];
    
    _fontArray = [NSArray arrayWithObjects:@"10",@"20",@"50",@"100",@"150",@"200", nil];
}

/** 初始化界面*/
- (void)_setupUIViewWithSuperViewFrame:(CGRect)frame{
    
    NSString *textColorTitle = [[NSUserDefaults standardUserDefaults] objectForKey:kTextColorTitle];
    NSString *textFontTitle = [[NSUserDefaults standardUserDefaults] objectForKey:kTextFontTitle];
    NSString *bgColorTitle = [[NSUserDefaults standardUserDefaults] objectForKey:kBgColorTitle];
    
    NSDictionary *textColorDic = [[NSUserDefaults standardUserDefaults] objectForKey:kTextColor];
    
    NSInteger textFont = [textFontTitle integerValue];
    
    NSDictionary *bgColorDic = [[NSUserDefaults standardUserDefaults] objectForKey:kBgColor];
    
    if (textColorDic != nil) {
        
        CGFloat r = [textColorDic[@"R"] floatValue];
        CGFloat g = [textColorDic[@"G"] floatValue];
        CGFloat b = [textColorDic[@"B"] floatValue];
        UIColor *textColor = [UIColor colorWithRed:r green:g blue:b alpha:1.0];
        _textColor = textColor;
    }
    if (textFont != 0) {
        
        _textFont = textFont;
    }
    if (bgColorDic != nil) {
        
        CGFloat r1 = [bgColorDic[@"R"] floatValue];
        CGFloat g1 = [bgColorDic[@"G"] floatValue];
        CGFloat b1 = [bgColorDic[@"B"] floatValue];
        UIColor *bgColor = [UIColor colorWithRed:r1 green:g1 blue:b1 alpha:1.0];
        _bgColor = bgColor;
    }
    
    //文字颜色
    _textColorOrderList = [[OrderListView alloc] initWithFrame:CGRectZero];
    if (textColorTitle != nil) {
        
        _textColorOrderList.orderListTitle = textColorTitle;
    }else{
        
        _textColorOrderList.orderListTitle = @"文字颜色";
    }
    _textColorOrderList.orderListImageName = @"下拉三角";
    [self addSubview:_textColorOrderList];
    
    //文字字体
    _textFontOrderList = [[OrderListView alloc] initWithFrame:CGRectZero];
    if (textFontTitle != nil) {
        
        _textFontOrderList.orderListTitle = textFontTitle;
    }else{
        
        _textFontOrderList.orderListTitle = @"文字字体";
    }
    _textFontOrderList.orderListImageName = @"下拉三角";
    [self addSubview:_textFontOrderList];
    
    //背景颜色
    _bgColorOrderList = [[OrderListView alloc] initWithFrame:CGRectZero];
    if (bgColorTitle != nil) {
        
        _bgColorOrderList.orderListTitle = bgColorTitle;
    }else{
        
        _bgColorOrderList.orderListTitle = @"背景颜色";
    }
    _bgColorOrderList.orderListImageName = @"下拉三角";
    [self addSubview:_bgColorOrderList];
    
    //添加三个UIControl
    [self _setupThreeUIControl];
    
    //取消按钮
    _cancelBtn = [[UIButton alloc] initWithFrame:CGRectZero];
    [_cancelBtn setImage:[UIImage imageNamed:@"cancel"] forState:UIControlStateNormal];
    [_cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_cancelBtn];
    
    //设定按钮
    _sureBtn = [[UIButton alloc] initWithFrame:CGRectZero];
    _sureBtn.backgroundColor = [UIColor greenColor];
    [_sureBtn setTitle:@"设定" forState:UIControlStateNormal];
    [_sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_sureBtn addTarget:self action:@selector(sureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_sureBtn];
}

/** 添加三个UIControl*/
- (void)_setupThreeUIControl{
    
    _textColorControl = [[UIControl alloc] initWithFrame:CGRectZero];
    _textColorControl.backgroundColor = [UIColor clearColor];
    [_textColorControl addTarget:self action:@selector(textColorAction:) forControlEvents:UIControlEventTouchUpInside];
    _textColorControl.selected = YES;
    [self addSubview:_textColorControl];
    
    _textFontControl = [[UIControl alloc] initWithFrame:CGRectZero];
    _textFontControl.backgroundColor = [UIColor clearColor];
    [_textFontControl addTarget:self action:@selector(textFontAction:) forControlEvents:UIControlEventTouchUpInside];
    _textFontControl.selected = YES;
    [self addSubview:_textFontControl];
    
    _bgColorControl = [[UIControl alloc] initWithFrame:CGRectZero];
    _bgColorControl.backgroundColor = [UIColor clearColor];
    [_bgColorControl addTarget:self action:@selector(bgColorAction:) forControlEvents:UIControlEventTouchUpInside];
    _bgColorControl.selected = YES;
    [self addSubview:_bgColorControl];
}

/** 点击文字颜色*/
- (void)textColorAction:(UIControl *)control{
    
    //  改变按钮的选中状态
    _textColorSelect = !_textColorSelect;
    
    //  根据按钮的选中状态设置下拉列表的收与合
    if (_textColorSelect) {
        
        _textColorTableView = [[UITableView alloc] initWithFrame:CGRectMake(_textColorOrderList.frame.origin.x, CGRectGetMaxY(_textColorOrderList.frame), _textColorOrderList.bounds.size.width, 5 * 44) style:UITableViewStylePlain];
        _textColorTableView.layer.borderWidth = 1.5;
        _textColorTableView.layer.borderColor = [UIColor whiteColor].CGColor;
        _textColorTableView.tag = 111;  // 这里根据tag值来获取不同的tableview
        _textColorTableView.delegate = self;
        _textColorTableView.dataSource = self;
        _textColorTableView.showsVerticalScrollIndicator = NO;
        [self addSubview:_textColorTableView];
        
        if (_textFontTableView)
        {
            [_textFontTableView removeFromSuperview];
            _textFontSelect = NO;
        }
        if (_bgColorTableView)
        {
            [_bgColorTableView removeFromSuperview];
            _bgColorSelect = NO;
        }
    }else{
        
        [_textColorTableView removeFromSuperview];
    }
    
}

/** 点击文字字体*/
- (void)textFontAction:(UIControl *)control{
    
    //  改变按钮的选中状态
    _textFontSelect = !_textFontSelect;
    
    //  根据按钮的选中状态设置下拉列表的收与合
    if (_textFontSelect) {
        
        _textFontTableView = [[UITableView alloc] initWithFrame:CGRectMake(_textFontOrderList.frame.origin.x, CGRectGetMaxY(_textFontOrderList.frame), _textFontOrderList.bounds.size.width, 5 * 44) style:UITableViewStylePlain];
        _textFontTableView.layer.borderWidth = 1.5;
        _textFontTableView.layer.borderColor = [UIColor whiteColor].CGColor;
        _textFontTableView.tag = 222;  // 这里根据tag值来获取不同的tableview
        _textFontTableView.delegate = self;
        _textFontTableView.dataSource = self;
        _textFontTableView.showsVerticalScrollIndicator = NO;
        [self addSubview:_textFontTableView];
        
        if (_textColorTableView)
        {
            [_textColorTableView removeFromSuperview];
            _textColorSelect = NO;
        }
        if (_bgColorTableView)
        {
            [_bgColorTableView removeFromSuperview];
            _bgColorSelect = NO;
        }
    }else{
        
        [_textFontTableView removeFromSuperview];
    }
}

/** 点击背景颜色*/
- (void)bgColorAction:(UIControl *)control{
    
    //  改变按钮的选中状态
    _bgColorSelect = !_bgColorSelect;
    
    //  根据按钮的选中状态设置下拉列表的收与合
    if (_bgColorSelect) {
        
        _bgColorTableView = [[UITableView alloc] initWithFrame:CGRectMake(_bgColorOrderList.frame.origin.x, CGRectGetMaxY(_bgColorOrderList.frame), _bgColorOrderList.bounds.size.width, 5 * 44) style:UITableViewStylePlain];
        _bgColorTableView.layer.borderWidth = 1.5;
        _bgColorTableView.layer.borderColor = [UIColor whiteColor].CGColor;
        _bgColorTableView.tag = 333;  // 这里根据tag值来获取不同的tableview
        _bgColorTableView.delegate = self;
        _bgColorTableView.dataSource = self;
        _bgColorTableView.showsVerticalScrollIndicator = NO;
        [self addSubview:_bgColorTableView];
        
        if (_textColorTableView)
        {
            [_textColorTableView removeFromSuperview];
            _textColorSelect = NO;
        }
        if (_textFontTableView)
        {
            [_textFontTableView removeFromSuperview];
            _textFontSelect = NO;
        }
    }else{
        
        [_bgColorTableView removeFromSuperview];
    }
}

/** 取消按钮点击事件*/
- (void)cancelBtnClick{
    
    [self removeFromSuperview];
}

/** 设定按钮点击事件*/
- (void)sureBtnClick:(UIButton *)btn{
    
    if ([self.delegation respondsToSelector:@selector(settingWithTextColor:textFont:bgColor:)]) {
        
        if (_textColor == nil && _textFont == 0 && _bgColor == nil) {
            
            [self removeFromSuperview];
            return;
        }
        else{
            
            if (_textColor == nil) {
                
                _textColor = [UIColor whiteColor];
            }
            if (_textFont == 0) {
                
                _textFont = 20;
            }
            if (_bgColor == nil) {
                
                _bgColor = [UIColor blackColor];
            }
            
            //保存状态
            [[NSUserDefaults standardUserDefaults] setObject:_textColorOrderList.orderListTitle forKey:kTextColorTitle];
            [[NSUserDefaults standardUserDefaults] setObject:_textFontOrderList.orderListTitle forKey:kTextFontTitle];
            [[NSUserDefaults standardUserDefaults] setObject:_bgColorOrderList.orderListTitle forKey:kBgColorTitle];
            
            NSDictionary *textColorDic = [self _getRGBDictionaryByColor:_textColor];
            NSDictionary *bgColorDic = [self _getRGBDictionaryByColor:_bgColor];
            
            [[NSUserDefaults standardUserDefaults] setObject:textColorDic forKey:kTextColor];
            [[NSUserDefaults standardUserDefaults] setObject:bgColorDic forKey:kBgColor];
            
            [self.delegation settingWithTextColor:_textColor textFont:_textFont bgColor:_bgColor];
        }
    }
    
    [self removeFromSuperview];
}

//将UIColor转换为RGB值
- (NSDictionary *)_getRGBDictionaryByColor:(UIColor *)originColor
{
    CGFloat r=0,g=0,b=0;
    CGColorRef color = [originColor CGColor];
    size_t numComponents = CGColorGetNumberOfComponents(color);
    
    if (numComponents == 4)
    {
        const CGFloat *components = CGColorGetComponents(color);
        r = components[0];
        g = components[1];
        b = components[2];
    }
    
    return @{@"R":@(r),
             @"G":@(g),
             @"B":@(b)};
}

/** 设置子控件的frame*/
- (void)layoutSubviews{
    
    CGFloat orderListWidth = (kSuperViewWidth - 4 * kEdge) / 3.0;
    
    _textColorOrderList.frame = CGRectMake(kEdge, 50, orderListWidth, 30);
    _textFontOrderList.frame = CGRectMake(CGRectGetMaxX(_textColorOrderList.frame)+kEdge, 50, orderListWidth, 30);
    _bgColorOrderList.frame = CGRectMake(CGRectGetMaxX(_textFontOrderList.frame)+kEdge, 50, orderListWidth, 30);
    
    _textColorControl.frame = _textColorOrderList.frame;
    _textFontControl.frame = _textFontOrderList.frame;
    _bgColorControl.frame = _bgColorOrderList.frame;
    
    _cancelBtn.frame = CGRectMake(kSuperViewWidth-40, 0, 40, 40);
    
    _sureBtn.frame = CGRectMake((kSuperViewWidth-100)*0.5, kSuperViewHeight-50, 100, 40);
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView.tag == 222) {
        
        return _fontArray.count;
    }else{
        
        return _colorArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *usedID = @"orderlist";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:usedID];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:usedID];
    }
    if (tableView.tag == 111) {
        
        cell.textLabel.text = _colorArray[indexPath.row][@"name"];
        
    }if (tableView.tag == 222) {
        
        cell.textLabel.text = _fontArray[indexPath.row];
    }if (tableView.tag == 333) {
        
        cell.textLabel.text = _colorArray[indexPath.row][@"name"];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:15.0];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView.tag == 111) {
        
        _textColorOrderList.orderListTitle = _colorArray[indexPath.row][@"name"]; // 重新给按钮的title赋值
        _textColor = _colorArray[indexPath.row][@"color"];
        [_textColorOrderList setNeedsLayout];
        _textColorSelect = !_textColorSelect;  //  注意这里需要改变按钮的选中状态
        [_textColorTableView removeFromSuperview];
    }
    if (tableView.tag == 222) {
        
        _textFontOrderList.orderListTitle = _fontArray[indexPath.row]; // 重新给按钮的title赋值
        _textFont = [_textFontOrderList.orderListTitle integerValue];
        [_textFontOrderList setNeedsLayout];
        _textFontSelect = !_textFontSelect;  //  注意这里需要改变按钮的选中状态
        [_textFontTableView removeFromSuperview];
    }
    if (tableView.tag == 333) {
        
        _bgColorOrderList.orderListTitle = _colorArray[indexPath.row][@"name"]; // 重新给按钮的title赋值
        _bgColor = _colorArray[indexPath.row][@"color"];
        [_bgColorOrderList setNeedsLayout];
        _bgColorSelect = !_bgColorSelect;  //  注意这里需要改变按钮的选中状态
        [_bgColorTableView removeFromSuperview];
    }
}

@end
